<?php

namespace App\HttpController\XApp\Cashbook\Admin;


use App\HttpController\ControllerAdmin;
use App\Model\t_a_cashbook_record;
use App\Model\t_wx_user;
use App\Traits\TraitExcel;
use XWX\Common\H;

class Cashbook extends ControllerAdmin
{
    use TraitExcel;


    public function index()
    {
        $this->funcSetTitle('账单-明细');


        $compKw = $this->reqParam('compKw');
        $compPrice = $this->reqParam('compPrice');

        $query = $this->db()
            ->join(t_wx_user::table(), 'cashrec_wxuser_id = wxuser_openid', 'INNER')
            ->where('cashrec_x_id', $this->funcGetX())
            ->orderBy('cashrec_create_date')
            ->orderBy('cashrec_id');

        //用户名
        if (H::funcStrHasAnyText($compKw))
        {
            $query->where('wxuser_nickname', "%{$compKw}%", 'LIKE');
        }

        //金额
        if (H::funcStrHasAnyText($compPrice))
        {
            $query->where('wxuser_nickname', "%{$compKw}%", 'LIKE');
        }


        if (!$this->funcIsAdminExport())
        {
            $query_count = clone $query;

            $list = $query->get(t_a_cashbook_record::table(), [$this->funcGetPageIndexXPage(), $this->funcGetPageSize()]);
            $count = $query_count->count(t_a_cashbook_record::table());


            //二次加工
            $list_ = [];
            foreach ($list as $v)
            {
                $data = $v;

                $entity = new t_a_cashbook_record($v);
                $data['id'] = $entity->funcGetID();
                $data['date'] = $entity->cashrec_date->toDateString();


                $list_[] = $data;
            }


            $this->commonR()->setData("list", $list_);
            $this->commonR()->setData("count", $count);


            $this->funcWriteCommonR();
        }
        else
        {
            //导出
            $table_head = [
                'cashrec_id' => 'ID',
                'cashrec_date' => '日期',
                'cashrec_wxuser_id' => '用户ID',
                'cashrec_desc' => '描述',
                'cashrec_num' => '金额',
            ];


            $this->funcExport($query, t_a_cashbook_record::table(), $table_head);
        }
    }


    //删除
    public function del()
    {
        $ids = $this->reqParam('ids');

        $r = $this->funcDel(t_a_cashbook_record::class, $ids);


        $this->funcWriteErrR($r);
    }


    /** 导出 */
    public function export()
    {
        $this->index();
    }
}